package it.antonio.ilpizze.db.core;

import it.antonio.ilpizze.db.core.fs.TransactionFile;

import java.util.List;

public class RecoverManager {
	private List<TransactionFile> transactions;
	
	public RecoverManager(List<TransactionFile> transactions) {
		this.transactions = transactions;
	}

	public void tryRecover(RecordRecoverManager recoverManager) {
		for (TransactionFile file : transactions) {
			if(file.isActive()){
				while (file.position() <= file.size() - 1) {
					byte type = file.readByte();
					if (type == RecordRecoverManager.REMOVE_RECOVER) {
						long position = file.readLong();
						recoverManager.recoverRemove(position);
						continue;
					}
					if (type == RecordRecoverManager.SAVE_RECOVER) {
						long position = file.readLong();
						recoverManager.recoverSave(position);
						continue;
					}
					break;
				}
				
			}
			
			file.close();
			while (file.delete());
		}

	}

}
